package com.easyattendance.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.easyattendance.model.AssignmentStudent;
import com.easyattendance.model.AttendanceTakenBy;
import com.easyattendance.model.EmailSettings;
import com.easyattendance.model.Parent;
import com.easyattendance.model.StudentAttendance;
import com.easyattendance.model.Teacher;
import com.easyattendance.services.AddClassService;
import com.easyattendance.services.AddStudentService;
import com.easyattendance.services.AddTeacherService;
import com.easyattendance.services.AssignmentSubmissionService;
import com.easyattendance.services.AttendanceSubmissionService;

@WebServlet(name = "AssignmentSubmissionServlet", urlPatterns = "/AssignmentSubmissionServlet")
public class AssignmentSubmissionServlet extends HttpServlet{
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		//HttpSession session = null;
		
		AssignmentSubmissionService assignmentService=new AssignmentSubmissionService();
		
		String stud_id[] =request.getParameterValues("stud_roll_no");
		String status[] = new String[stud_id.length];
		String class_id=request.getParameter("class_id");
		String subject_id=request.getParameter("subject_id");
		String assignment_id=request.getParameter("assignment_id");
		
		for (int i = 0; i < stud_id.length; i++) {
			status[i] = request.getParameter("status" + i);

			if (status[i].equals("submitted"))
				status[i] = "Y";
			else if (status[i].equals("not_submitted"))
				status[i] = "N";
			
		}
		
		String marks_received[]=request.getParameterValues("marks_received");

		int id = 0;
		Date current_date = null;
		String status_str = null;
		
		
		boolean result = false;
		for (int i = 0; i < stud_id.length; i++) {
			result = false;
			id = Integer.parseInt(stud_id[i]);
			
			current_date = new Date();
			status_str = status[i];
			
			int marks;
			if(marks_received[i].equals(""))
				 marks=0;
			else
				 marks=Integer.parseInt(marks_received[i]);
			
			AssignmentStudent assignment_stud = new AssignmentStudent(id,assignment_id,subject_id,class_id,status_str,current_date,marks);
			
			boolean res=assignmentService.ifAlreadyExists(assignment_stud);
			
			if (!res) {
				result = assignmentService.saveAssignmentSubmission(assignment_stud);
			}
			else {
				result = assignmentService.updateAssignmentSubmission(assignment_stud);
			}

			if (result == false) {
				break;
			}

		}
		
			try {
				if (result) {
					
					/*request.setAttribute("assignmentSaved", "Assignment Submission records saved Successfully");
		        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/updateAssignmentSubmission.jsp");
		        	rd.forward(request, response); */
					
					request.getSession().setAttribute("assignmentSaved", "Assignment Submission records saved Successfully.");
					response.sendRedirect("views/teacher/updateAssignmentSubmission.jsp");
				} else {
					
					/*request.setAttribute("assignmentNotSaved", "Assignment Submission records could not be saved successfully");
		        	RequestDispatcher rd = request.getRequestDispatcher("views/teacher/updateAssignmentSubmission.jsp");
		        	rd.forward(request, response); */
					
					request.getSession().setAttribute("assignmentNotSaved", "Assignment Submission records could not be saved.");
					response.sendRedirect("views/teacher/updateAssignmentSubmission.jsp");
				}
			} finally {
			}
	  }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
